<template>
  <el-container class="full-container">
    <el-main class="main">
      <el-row class="row-container">
        <!-- 登录卡片，增加最大宽度限制 -->
        <el-col :span="24" :lg="8" :md="12" :sm="16">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span class="login-title">用户登录</span>
            </div>

            <!-- 登录表单 -->
            <el-form
                :model="loginForm"
                :rules="loginRules"
                ref="loginForm"
                class="login-form"
            >
              <el-form-item prop="username">
                <el-row :gutter="10" class="form-item">
                  <el-col :span="6" class="label-col">
                    <span class="label">用户名:</span>
                  </el-col>
                  <el-col :span="18">
                    <el-input
                        v-model="loginForm.adUsername"
                        placeholder="请输入用户名"
                        prefix-icon="el-icon-user"
                        autocomplete="off"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-form-item>

              <el-form-item prop="password">
                <el-row :gutter="10" class="form-item">
                  <el-col :span="6" class="label-col">
                    <span class="label">密 &nbsp;&nbsp; 码:</span>
                  </el-col>
                  <el-col :span="18">
                    <el-input
                        v-model="loginForm.password"
                        placeholder="请输入密码"
                        type="password"
                        prefix-icon="el-icon-lock"
                        show-password
                        autocomplete="off"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-form-item>

              <!-- 记住密码和忘记密码 -->
              <el-row class="form-actions">
                <el-col :span="12">
                  <el-checkbox v-model="rememberMe" class="remember-me">记住密码</el-checkbox>
                </el-col>
                <el-col :span="12" class="text-right">
                  <a href="#" class="forgot-password">忘记密码?</a>
                </el-col>
              </el-row>

              <!-- 登录按钮 -->
              <el-form-item class="submit-btn">
                <el-button
                    type="primary"
                    class="login-btn"
                    @click="handleLogin"
                    :loading="loginLoading"
                >
                  登录
                </el-button>
              </el-form-item>

            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script>
import axios from "axios";

export default {
  name: "LoginPage",
  data() {
    // 用户名验证规则
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入用户名'));
      }
      callback();
    };

    // 密码验证规则
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'));
      }
      // if (value.length < 6) {
      //   return callback(new Error('密码长度不能少于6位'));
      // }
      callback();
    };

    return {
      loginForm: {
        adUsername: '',
        password: ''
      },
      rememberMe: false,
      loginLoading: false,
      loginRules: {
        emUsername: [{validator: validateUsername, trigger: 'blur'}],
        password: [{validator: validatePassword, trigger: 'blur'}]
      }
    };
  },
  methods: {
    // 处理登录
    handleLogin() {

      axios.post("http://localhost:8080/admin/login", this.loginForm)
          .then(res => {
            const code = res.data.code;
            if (code === 200) {
              this.loginLoading = true;
              // 模拟登录请求
              setTimeout(() => {
                this.loginLoading = false;
                this.$message.success('登录成功');
                this.$store.state.token = res.data.data
                console.log(this.$store.state.token)
                // 这里可以添加路由跳转逻辑
                this.$router.push('/admin');
              }, 1500);
            }else if(code === 402){
              this.$message.error('密码错误');
              return false;
            }else if (code === 401){
              this.$message.error('用户不存在');
              return false;
            }else {
              this.$message.error('请完善登录信息');
              return false;
            }
          }).catch(function (error) {
            console.log(error)
          })
      // this.$refs.loginForm.validate((valid) => {
      //
      //   if (valid) {
      //     this.loginLoading = true;
      //     // 模拟登录请求
      //     setTimeout(() => {
      //       this.loginLoading = false;
      //       this.$message.success('登录成功');
      //       // 这里可以添加路由跳转逻辑
      //       // this.$router.push('/home');
      //     }, 1500);
      //   } else {
      //     this.$message.error('请完善登录信息');
      //     return false;
      //   }
      // });
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
}

.full-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.main {
  flex: 1;
  width: 100%;
  padding: 0;
  margin: 0;
  background-image: url("https://img-s.msn.cn/tenant/amp/entityid/BB1msIHt?w=0&h=0&q=60&m=6&f=jpg&u=t");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  overflow: hidden;
  /* 增加背景遮罩，提升文字可读性 */
  position: relative;
}

.main::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 1;
}

.row-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  z-index: 2; /* 确保卡片在遮罩上方 */
}

.box-card {
  background-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  padding: 30px 25px;
  width: 100%;
  max-width: 420px;
  /* 添加卡片动画效果 */
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.box-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.25);
}

.login-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  display: block;
  text-align: center;
  margin-bottom: 10px;
}

.login-form {
  margin-top: 20px;
}

.form-item {
  margin-bottom: 22px;
  align-items: center;
}

.label-col {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.label {
  font-size: 14px;
  color: #606266;
  font-weight: 500;
}

/* 表单操作区 */
.form-actions {
  margin: 10px 0 25px;
  font-size: 13px;
}

.remember-me {
  color: #606266;
}

.forgot-password {
  color: #409EFF;
  text-decoration: none;
  transition: color 0.2s;
}

.forgot-password:hover {
  color: #66b1ff;
  text-decoration: underline;
}

/* 登录按钮 */
.submit-btn {
  margin-top: 10px;
}

.login-btn {
  width: 100%;
  height: 40px;
  font-size: 15px;
  letter-spacing: 1px;
  transition: all 0.3s ease;
}

/* 其他登录方式 */
.other-login {
  margin-top: 25px;
  text-align: center;
}

.divider {
  display: inline-block;
  color: #c0c4cc;
  font-size: 12px;
  margin-bottom: 15px;
  position: relative;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 60px;
  height: 1px;
  background-color: #e5e6eb;
}

.divider::before {
  left: -70px;
}

.divider::after {
  right: -70px;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.social-btn {
  width: 40px;
  height: 40px;
  transition: transform 0.2s ease;
}

.social-btn:hover {
  transform: scale(1.1);
}

.qq-btn {
  color: #12B7F5;
  border-color: #12B7F5;
}

.wx-btn {
  color: #07C160;
  border-color: #07C160;
}



</style>
